package com.aier.proxy.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.proxy.entity.ProxyUser;

public class ProxyUserRowMapper implements RowMapper<ProxyUser> {

    @SuppressWarnings("unused")
    private int type;

    public ProxyUserRowMapper() {
    }

    public ProxyUserRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ProxyUser mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseProxyUser(new ProxyUser(), rs);
    }

    public static ProxyUser parseProxyUser(ProxyUser pu, ResultSet rs) throws SQLException {
        return parseProxyUser(pu, rs, null);
    }

    public static ProxyUser parseProxyUser(ProxyUser pu, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            pu.setPid(rs.getInt(su+"pid"));
        } catch (SQLException e) {
            pu.setPid(null);
        }
        try {
            pu.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            pu.setUid(null);
        }
        
        return pu;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ProxyUser pu) {
        args.put("pid",pu.getPid());
        args.put("uid", pu.getUid());
    
        return args;
    }
    
    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ProxyUser proxyUser) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(proxyUser),
                        new String[] { "pid" });
                parsePS(ps, proxyUser);
                return ps;
            }

        };
    }

    private static String parseSQL(ProxyUser proxyUser) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into proxy_user(");
        int count = 0;
        if (null != proxyUser.getUid()) {
            sql.append("uid,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }
    
    private static void parsePS(PreparedStatement ps, ProxyUser proxyUser)
        throws SQLException {
        int count = 1;
        if (null != proxyUser.getUid()) {
            ps.setLong(count++, proxyUser.getUid());
        }
    }
    
    
}
